#!/bin/bash

ARGS=("$@")

if [ ${#ARGS[@]} -ne 10 ]
then
    echo "Error: incorrect number of arguments."
    echo "Usage: test.sh config_file ds_file ds_tree_format ds_file_format ps_file ps_tree_format ps_file_format unary_rules binary_rules output_dir"
    exit
fi

script_dir=$(dirname $0)
dist=$script_dir/../bin
pkg=edu.washington.ling.syntric.proc
jar_dir=$script_dir/../jars

config_file=${ARGS[0]}
ds_file=${ARGS[1]}
ds_tree_format=${ARGS[2]}
ds_file_format=${ARGS[3]}
ps_file=${ARGS[4]}
ps_tree_format=${ARGS[5]}
ps_file_format=${ARGS[6]}
unary_rules=${ARGS[7]}
binary_rules=${ARGS[8]}
output_dir=${ARGS[9]}

# Set up output directory
if [ ! -d $output_dir ]; then
	if ! mkdir $output_dir ; then
		echo Error: output_dir=\'$output_dir\' was not valid
		echo Please specify a valid output directory
		exit
	fi
fi

echo %% 10 Checking DS Format
echo %%
if [ -d $output_dir/10-check-ds ]; then
	rm -rf $output_dir/10-check-ds
fi
mkdir $output_dir/10-check-ds
java -cp $dist:$jar_dir/* $pkg.CheckDSFormat $ds_file $output_dir/10-check-ds/ds-good.txt $output_dir/10-check-ds/ds-bad.txt --0t $ds_tree_format --0f $ds_file_format --1f sgml --2f sgml 2> $output_dir/10-check-ds/errors.txt

echo 
echo %%
echo %% 10 Checking PS Format
echo %%
if [ -d $output_dir/10-check-ps ]; then
	rm -rf $output_dir/10-check-ps
fi
mkdir $output_dir/10-check-ps
java -cp $dist:$jar_dir/* $pkg.CheckPSFormat $ps_file $output_dir/10-check-ps/ps-good.txt $output_dir/10-check-ps/ps-bad.txt --0t $ps_tree_format --0f $ps_file_format --1f sgml --2f sgml 2> $output_dir/10-check-ps/errors.txt


echo 
echo %%
echo %% 20 Merging DS and PS
echo %%
if [ -d $output_dir/20-merge ]; then
	rm -rf $output_dir/20-merge
fi
mkdir $output_dir/20-merge
java -cp $dist:$jar_dir/* $pkg.MergeDSandPS $output_dir/10-check-ds/ds-good.txt $output_dir/10-check-ps/ps-good.txt $output_dir/20-merge/cs-good.txt --0t $ps_tree_format --0f sgml --1t $ds_tree_format --1f sgml --2t ssf --2f sgml --config $config_file 2> $output_dir/20-merge/errors.txt

echo 
echo %%
echo %% 25 Attaching PS empty categories to DS
echo %%
if [ -d $output_dir/25-attach-traces ]; then
	rm -rf $output_dir/25-attach-traces
fi
mkdir $output_dir/25-attach-traces
java -cp $dist:$jar_dir/* $pkg.AttachTracesToDS $output_dir/20-merge/cs-good.txt $output_dir/25-attach-traces/cs-plus.txt --0t ssf --0f sgml --1t ssf --1f sgml 2> $output_dir/25-attach-traces/errors.txt

echo 
echo %%
echo %% 30 Checking consistency between DS and PS
echo %%
if [ -d $output_dir/30-check-consist ]; then
	rm -rf $output_dir/30-check-consist
fi
mkdir $output_dir/30-check-consist
java -cp $dist:$jar_dir/* $pkg.CheckConsistency $output_dir/25-attach-traces/cs-plus.txt $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/30-check-consist/cs-plus.inconst.txt --0t ssf --0f sgml --1t ssf --1f sgml --2t ssf --2f sgml 2> $output_dir/30-check-consist/errors.txt


echo %%
echo %% Applying rules to get PS trees
echo %%
if [ -d $output_dir/60-apply-rules ]; then
	rm -rf $output_dir/60-apply-rules
fi
mkdir $output_dir/60-apply-rules
java -cp $dist:$jar_dir/* $pkg.ApplyRules $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/60-apply-rules/output $unary_rules $binary_rules --0s cs --0t ssf --0f sgml --1t ptb --1f flat --config config.xml 2> $output_dir/60-apply-rules/errors.txt

